package util;

import dao.impl.CaseHistoryDaoImpl;
import dao.impl.DoctorDaoImpl;
import dao.impl.PatientDaoImpl;
import entity.Doctor;
import entity.DoctorBaseInfo;
import entity.Patient;
import entity.PatientBaseInfo;

import java.text.ParseException;

/**
 * @Author:
 * @Date: 2020-04-08
 * @Description: @PACKAGE:cn.tianbao.dao
 * @version: 1.0
 */

/**
 * id :
 * name :
 * age :
 * birthday :
 * fistTime :
 * sex :
 * tel :
 * familyName :
 * familyTel :
 */
public class GetBaseInfo {

    public static PatientBaseInfo GetPatientIdInfo(String id) throws ParseException {
        int flag = 1;//自定运行状态码，初始为1：正常，由数据库查询未找到病人错误：0，
        Patient patient = new Patient();
        PatientDaoImpl patientDao = new PatientDaoImpl();
        CaseHistoryDaoImpl caseHistoryDao = new CaseHistoryDaoImpl();
        PatientBaseInfo patientBaseInfo = new PatientBaseInfo();
        if (id.length()!=18){
            flag = 2;
            patientBaseInfo.setFlag(flag);
            return patientBaseInfo;
        }
        try {
            patient = patientDao.getByPatientId(id).get(0);
            System.out.println();
            patientBaseInfo = GetBaseInfo.pBase(id);
            patientBaseInfo.setName(patient.getName());
            try {
                patientBaseInfo.setFistTime(caseHistoryDao.getByPatientIdOrderByDate(id).get(0).getCh_date());
            } catch (Exception e) {
                patientBaseInfo.setFistTime(DateUtil.getStringDateShort());
            }
            patientBaseInfo.setTel(patient.getTel_Number());
            patientBaseInfo.setFamilyName(patient.getFamily_Name());
            patientBaseInfo.setFlag(flag);
            patientBaseInfo.setPatientExsit(true);
            patientBaseInfo.setMsg("病人信息存在，请录入病历");
            return patientBaseInfo;
        } catch (Exception e) {
            flag = 0;
            patientBaseInfo = GetBaseInfo.pBase(id);
            patientBaseInfo.setFistTime(DateUtil.getStringDateShort());
            patientBaseInfo.setFlag(flag);
            patientBaseInfo.setPatientExsit(false);
            patientBaseInfo.setMsg("病人信息不存在，请继续录入");
            return patientBaseInfo;
        }finally {
            return patientBaseInfo;
        }

    }

    public static DoctorBaseInfo GetDoctorIdInfo(String id) throws ParseException {
        int flag = 1;//自定运行状态码，初始为1：正常，由数据库查询未找到错误：0；传入ID不正确 2 ；
        Doctor doctor = new Doctor();
        DoctorDaoImpl doctorDao = new DoctorDaoImpl();
        DoctorBaseInfo doctorBaseInfo = new DoctorBaseInfo();
        if (id.length()!=18){
            flag = 2;
            doctorBaseInfo.setFlag(flag);
            return doctorBaseInfo;
        }
        try {//存在
            doctor = doctorDao.getByDoctorId(id).get(0);
            System.out.println();
            doctorBaseInfo = GetBaseInfo.dBase(id);
            doctorBaseInfo.setId(id);
            doctorBaseInfo.setName(doctor.getName());
            try {
                doctorBaseInfo.setJoindate(doctorDao.getByDoctorId(id).get(0).getJoindate());
            } catch (Exception e) {
                doctorBaseInfo.setJoindate(DateUtil.getStringDateShort());
            }
            doctorBaseInfo.setTel(doctor.getTel());
            doctorBaseInfo.setEdu(doctor.getEdu());
            doctorBaseInfo.setOther(doctor.getOther());
            doctorBaseInfo.setPicture(doctor.getPicture());
            doctorBaseInfo.setPost(doctor.getPost());
            doctorBaseInfo.setEmail(doctor.getEmail());
            doctorBaseInfo.setJoindate(doctor.getJoindate());
            doctorBaseInfo.setDoctorExsit(true);
            doctorBaseInfo.setMsg("医生信息存在，您可直接修改关键信息");
            doctorBaseInfo.setFlag(flag);
            return doctorBaseInfo;
        } catch (Exception e) {
            //不存在
            flag = 0;
            doctorBaseInfo = GetBaseInfo.dBase(id);
            doctorBaseInfo.setId(id);
            doctorBaseInfo.setJoindate(DateUtil.getStringDateShort());
            doctorBaseInfo.setDoctorExsit(false);
            doctorBaseInfo.setMsg("医生信息不存在，请继续录入医生信息");
            doctorBaseInfo.setFlag(flag);
            return doctorBaseInfo;
        }finally {
            return doctorBaseInfo;
        }

    }
    public static PatientBaseInfo pBase(String id){
        PatientBaseInfo patientBaseInfo = new PatientBaseInfo();
        int sexnum = Integer.parseInt(id.substring(16, 17));
        String sex = "";
        if (sexnum % 2 == 0) {
            sex = "女";
        } else {
            sex = "男";
        }
        long age = 0;
        String birthday = id.substring(6, 14);
        birthday = birthday.substring(0, 4) + "-" + birthday.substring(4, 6) + "-" + birthday.substring(6, 8);
        System.out.println(birthday);
        age = DateUtil.getDays(DateUtil.getStringDateShort(), birthday);
        age /= 365;
        patientBaseInfo.setSex(sex);
        patientBaseInfo.setAge(String.valueOf(age));
        patientBaseInfo.setBirthday(birthday);
        return patientBaseInfo;
    }
    public static DoctorBaseInfo dBase(String id){
        DoctorBaseInfo doctorBaseInfo = new DoctorBaseInfo();
        int sexnum = Integer.parseInt(id.substring(16, 17));
        String sex = "";
        if (sexnum % 2 == 0) {
            sex = "女";
        } else {
            sex = "男";
        }
        doctorBaseInfo.setSex(sex);
        return doctorBaseInfo;
    }
}
